﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.ProviderLoader;
using PSE.Framework.ProviderLoader.Configuration;
using PSE.Framework.Configuration;
using System.Configuration;

namespace PSE.Framework.UI.Resources
{
    public static class ResourceProviderManager
    {
        private static IResourceProvider _resourceProvider = null;
        private static object obj = new object();

        public static IResourceProvider GetResourceProvider()
        {
            try
            {
                lock (obj)
                {
                    if (_resourceProvider == null)
                    {
                        ProviderConfigurationSection configSection = ApplicationConfiguration.GetCurrent().GetSection<ProviderConfigurationSection>("clientProviders");
                        
                        if (configSection != null)
                        {
                            _resourceProvider = ProviderLoader<IResourceProvider>.LoadProvider(configSection.Providers, "resourceProvider");
                        }
                        else
                        {
                            throw new ConfigurationErrorsException("Cannot find 'resourceProvider' in client providers.");
                        }
                    }
                }

                return _resourceProvider;
            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException("Could not create an instance of Resource Provider configured.", ex);
            }
        }
    }
}
